import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.exception.*;

import com.dao.SearchUsers;
import com.util.*;

import org.hibernate.cfg.Configuration;

import entities.*;
public class hiberateTest {

	public static Users connect()
	{
		//SessionFactory sf = new Configuration().configure().buildSessionFactory();
		
		//SearchUsers u = new SearchUsers();
		
			Session se = HbnUtil.getSession();
			
			if(se.isConnected());
			System.out.println("connected");
			//tran = se.beginTransaction();
			Users user=(Users)se.createQuery("from Users as u where u.email= 'Aliquam@rutrum.co.uk'").uniqueResult();
			return user;
	}
	
	@SuppressWarnings("unchecked")
	public static void read()
	{
				Transaction tran = null;
				SearchUsers u = new SearchUsers();
				
					Session se = HbnUtil.getSession();
					if(se.isConnected());
					System.out.println("connected");
					tran = se.beginTransaction();
					List<Users> list=(List<Users>)se.createQuery("from Users ").list();
					for(Users user : list)
					{
						System.out.println("Email: " + user.getEmail());
						System.out.println("Name: " + user.getName());
						System.out.println("Phone: " + user.getPhone());
						System.out.println("Type: " + user.getUsertype());
					}
					tran.commit();
					se.close();
					
	}
	public static void write()
	{
		Users user = new Users();
		//response.setContentType("text/html");
		//Administrate admin = new Administrate();
		Session se = HbnUtil.getSession();
		Transaction tran = se.beginTransaction();
		/*admin.setOrderid(new Long(9999));
		admin.setProductid(new Long(9999));
		admin.setSpecialofferid(new Long(88888));
		admin.setUserid(99999);*/
		//user.setUserid("999");
		user.setEmail("sw2848@columbia.edu");
		user.setName("siyang wu");
		user.setPassword("abc123");
		user.setPhone("3478783342");
		se.save(user);
		
		tran.commit();
		se.close();
	}
	public static void main(String [] args)
	{
		//write();
		read();
		
	}
}
